import { Component, OnInit, ChangeDetectionStrategy, OnDestroy, forwardRef} from '@angular/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS,FormGroup, FormControl,FormBuilder} from "@angular/forms"
import {IdentityType,Identity} from "../../domain";
import {Subject, Observable, combineLatest, Subscription} from "rxjs"
@Component({
  selector: 'app-identity-input',
  templateUrl: './identity-input.component.html',
  styleUrls: ['./identity-input.component.scss'],
  providers:[
    {
      provide:NG_VALUE_ACCESSOR,
      useExisting:forwardRef(()=>IdentityInputComponent),
      multi:true
    },
    {
      provide:NG_VALIDATORS,
      useExisting:forwardRef(()=>IdentityInputComponent),
      multi:true
    }
  ],
  changeDetection:ChangeDetectionStrategy.OnPush
})
export class IdentityInputComponent implements OnInit,ControlValueAccessor ,OnDestroy{
  private sub:Subscription;
  identityTypes = [
    {value:IdentityType.Idcard,label:"身份证"},
    {value:IdentityType.Insurance,label:"医保"},
    {value:IdentityType.Passport,label:"护照"},
    {value:IdentityType.Military,label:"军官证"},
    {value:IdentityType.Other,label:"其他"}
  ];
  identity:Identity = {identityType:null,identityNo:null};
  private propagateChange = (_:any)=>{};
  private _idType = new Subject<IdentityType>();
  private _idNo = new Subject<string>();
  constructor() { }

  ngOnInit() {
    const val$ = combineLatest(this.idNo,this._idType,(_no,_type)=>{
        return {
          identityType:_type,
          identityNo:_no
        }
    })
    this.sub = val$.subscribe(id=>{
      this.propagateChange(id);
    })
  }
  ngOnDestroy(){
    if(this.sub){
      this.sub.unsubscribe();
    }
  }
  onIdTypeChange(idType:IdentityType){
    this._idType.next(idType);
  }
  onIdNoChange(idNo:string){
    this._idNo.next(idNo);
  }
  get idType():Observable<IdentityType>{
    return this._idType.asObservable();  
  }
  get idNo():Observable<any>{
    return this._idNo.asObservable();
  }
  writeValue(obj: any): void{
     if(obj){
       this.identity = obj;
     }
  }
  registerOnChange(fn: any): void{
    this.propagateChange = fn;
  }
  registerOnTouched(fn: any): void{

  }
  validate(c:FormControl):{[key:string]:any}{
      const val  = c.value;
      if(!val){
        return null;
      }
      switch(val.identityType){
        case IdentityType.Idcard:
        return this.validateIdCard(); 
        case IdentityType.Passport:
        return this.validatePassport();
        case IdentityType.Military:
        return this.validateMilitary();
        case IdentityType.Insurance:
        default:{
          return null;
        }
      }
  }
  validateIdCard(c?:FormControl):{[key:string]:any}{
    const val  = c.value;
    if(val.length!==18){
      return {idInvalid:true};
    }
    const pattern = / /;
    return pattern.test(val) ? null : {idNotValid: true};
  }
  validatePassport(c?:FormControl):{[key:string]:any}{
    const val  = c.value;
    if(val.length!==9){
      return {idInvalid:true};
    }
    const pattern = / /;
    return pattern.test(val) ? null : {idNotValid: true};
  }
  validateMilitary(c?:FormControl):{[key:string]:any}{
    const val  = c.value;
    if(val.length!==9){
      return {idInvalid:true};
    }
    const pattern = / /;
    return pattern.test(val) ? null : {idNotValid: true};
  }
}
